var express = require('express');
var router = express.Router();
let { strategyModel,equipmentTypeyModel,equipmentModel } = require('../db/peng')
let {cooperativeModel,areaModel} = require('../db/tian')

/* GET home page. */
// 获取策略信息
router.get('/getstrategys', async(req, res, next)=>{
  let data = await strategyModel.find()
  res.send({
    code:200,
    data
  })
});
router.get('/getstrategy', async(req, res, next)=>{
  let name = RegExp(req.query.name)
  let page = req.query.page
  let size = req.query.size
  let data = await strategyModel.find({name:name}).skip((page-1)*size).limit(size)
  let count = await strategyModel.count()
  res.send({
    code:200,
    data,
    count
  })
});
// 添加策略信息
router.post('/poststrategy', async(req, res, next)=>{
  try {
    await strategyModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
// 删除策略信息
router.get('/delstrategy', async(req, res, next)=>{
  try {
    await strategyModel.deleteOne(req.query)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
// 修改策略信息
router.post('/putstrategy', async(req, res, next)=>{
  try {
    await strategyModel.updateOne({_id:req.body._id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});



// 获取设备类型信息
router.get('/getequipmentTypes', async(req, res, next)=>{
  let data = await equipmentTypeyModel.find()
  res.send({
    code:200,
    data,
  })
});
router.get('/getequipmentType', async(req, res, next)=>{
  let name = RegExp(req.query.name)
  let page = req.query.page
  let size = req.query.size
  let data = await equipmentTypeyModel.find({name:name}).skip((page-1)*size).limit(size)
  let count = await equipmentTypeyModel.count()
  res.send({
    code:200,
    data,
    count
  })
});
// 添加设备类型信息
router.post('/postequipmentType', async(req, res, next)=>{
  try {
    await equipmentTypeyModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
// 删除设备类型信息
router.get('/delequipmentType', async(req, res, next)=>{
  try {
    await equipmentTypeyModel.deleteOne(req.query)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
// 修改设备类型信息
router.post('/putequipmentType', async(req, res, next)=>{
  try {
    await equipmentTypeyModel.updateOne({_id:req.body._id},req.body)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});


// 获取设备信息
router.get('/getequipment', async(req, res, next)=>{
  let id = req.query._id
  let page = req.query.page
  let size = req.query.size
  if(id === ''){
    let data = await equipmentModel.find().populate('typeid').populate('ptid').skip((page-1)*size).limit(size)
    let count = await equipmentModel.count()
    // console.log(data);
    for (let index = 0; index < data.length; index++) {
      const element = data[index];
      // console.log(element);
      let cooper = await cooperativeModel.findOne({_id:data[index].ptid.cooperative})
      let area = await areaModel.findOne({_id:data[index].ptid.area})
      data[index].ptid.cooperative=cooper
      data[index].ptid.area=area
    }
    // console.log(data);  
    res.send({
      code:200,
      data,
      count
    })
  }else{
    let data = await equipmentModel.find({_id:req.query._id}).populate('typeid').populate('ptid').skip((page-1)*size).limit(size)
    let count = await equipmentModel.count()
    for (let index = 0; index < data.length; index++) {
      const element = data[index];
      // console.log(element);
      let cooper = await cooperativeModel.findOne({_id:data[index].ptid.cooperative})
      let area = await areaModel.findOne({_id:data[index].ptid.area})
      data[index].ptid.cooperative=cooper
      data[index].ptid.area=area
    }
    res.send({
      code:200,
      data,
      count
    })
  }
  
});
// 添加设备信息
router.post('/postequipment', async(req, res, next)=>{
  console.log(req.body);
  try {
    await equipmentModel.create(req.body)
    res.send({
      code:200
    })
  } catch (error) {
    console.log(error);
    res.send({
      code:500
    })
  }
});
// 删除设备信息
router.get('/delequipment', async(req, res, next)=>{
  try {
    await equipmentModel.deleteOne(req.query)
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
// 修改设备信息
router.post('/putequipment', async(req, res, next)=>{
  console.log(req.body);
  try {
    await equipmentModel.updateOne({_id:req.body._id},{ptid:req.body.ptid})
    res.send({
      code:200
    })
  } catch (error) {
    res.send({
      code:500
    })
  }
});
router.post('/putequipmentstr', async(req, res, next)=>{
  console.log(req.body);
  let list = req.body.list
  let strategyid = req.body.data.strategyid
  if (list.length>0) {
    list.forEach(async i => {
      await equipmentModel.updateOne({_id:i._id},{strategyid:strategyid})
    });
    res.send({
      code:200
    })
  }else{
    try {
      await equipmentModel.updateOne({_id:req.body.data._id},req.body.data)
      res.send({
        code:200
      })
    } catch (error) {
      res.send({
        code:500
      })
    }
  }
});
// 修改策略信息
router.get('/setstra', async(req, res, next)=>{
    let data = await equipmentModel.findOne(req.query)
    console.log(data);
    if (data.strategyid) {
      let datas = await strategyModel.findOne({_id:data.strategyid})
      console.log(datas);
      res.send({
        code:200,
        datas
      })
    }else{
      res.send({
        code:203
      })
    }
});
module.exports = router;
